package com.euond.permission.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.euond.common.core.dto.ResultDto;
import com.euond.common.web.controller.BaseCRUDController;
import com.euond.common.web.feign.integration.CompanyFeignService;
import com.euond.common.web.feign.uc.UserDto;
import com.euond.common.web.feign.uc.UserFeignService;
import com.euond.permission.entities.UserPrivilegeInfo;
import com.euond.permission.service.UserPrivilegeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2018/1/6.
 */
@RestController
@RequestMapping("permission/front/userprivilegeinfos")
public class UserPrivilegeInfoController extends BaseCRUDController<UserPrivilegeInfoService,UserPrivilegeInfo> {

    @Autowired
    UserFeignService userFeignService;

    @Autowired
    CompanyFeignService companyFeignService;

    /**
     * 按条件查询实体列表以分页返回(覆盖基类)
     */
    @GetMapping(value = "page")
    public ResultDto listPage(@RequestParam(required = false) Map<String, Object> searchParams) {
        ResultDto resultDto = null;
        Page<UserPrivilegeInfo> userPrivilegeInfoPage = baseService.selectListPageByMap(searchParams);
        if (userPrivilegeInfoPage.getTotal() > 0) {
            List<UserPrivilegeInfo> userPrivilegeInfoList = userPrivilegeInfoPage.getRecords();
            List<Long> userIds = new ArrayList<Long>();
            for (UserPrivilegeInfo userPrivilegeInfo : userPrivilegeInfoList) {
                //ID去重
                if (!userIds.contains(userPrivilegeInfo.getUserId())) {
                    userIds.add(userPrivilegeInfo.getUserId());
                }
            }
            List<UserDto> userDtoList = userFeignService.findUsersByIds(userIds);
            if (userDtoList.size() > 0) {
                for (int i = 0; i < userPrivilegeInfoList.size(); i++) {
                    //公司
                    for (UserDto userDto : userDtoList) {
                        if (userPrivilegeInfoList.get(i).getUserId().equals(userDto.getId())) {
                            userPrivilegeInfoList.get(i).setUsername(userDto.getUsername());
                        }
                    }
                }
                resultDto = new ResultDto("0", "seccess", userPrivilegeInfoList, userPrivilegeInfoPage.getTotal());
            } else {
                resultDto = new ResultDto("9009", "没有查到任何数据！");
            }
        }
        return resultDto;
    }

    /**
     * 根据数据类型获取对应数据源数据
     */
    @GetMapping(value = "datatype")
    public ResultDto findDataTypeSource(@RequestParam("dataType") int dataType) {
        ResultDto resultDto =null;
        List<UserPrivilegeInfo> userPrivilegeInfoList= new ArrayList<>();
        Map<Long,String> dataNames = new HashMap<>();
        if(dataType==1){
            dataNames=companyFeignService.findAllCompanyNames();
            if(dataNames.size()>0){
                //公司
                for (Map.Entry<Long,String> entry : dataNames.entrySet()) {
                    Long key = entry.getKey();
                    String value = entry.getValue();
                    UserPrivilegeInfo userPrivilegeInfo=new UserPrivilegeInfo();
                    userPrivilegeInfo.setDataType(dataType);
                    userPrivilegeInfo.setDataId(key);
                    userPrivilegeInfo.setDataName(value);
                    userPrivilegeInfoList.add(userPrivilegeInfo);
                }
                resultDto= new ResultDto("0","seccess",userPrivilegeInfoList);
            }else{
                resultDto = new ResultDto("9009", "没有查到任何数据！");
            }
        }
        return resultDto;
    }

}
